package com.skt.common_hr.login
{
	import com.wcl.dataloader.DataLoaderFactory;
	import com.wcl.dataloader.IDataLoader;
	
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class LoginProxy extends EventDispatcher
	{
		protected var loader:URLLoader = new URLLoader();

		public var retCode:int;
		public var retText:String;
		public var faultDetail:String;
		
		private var company:String = "SAC";
		public var language:String = "KO";
		
		public function LoginProxy(company:String)
		{
			if(company==null) {
				company = "SAC";
			}
			this.company = company;
		}
		
		public function get server():String
		{
			return company;
		}
		
		public function loadLoginInfo(pernr:String, password:String):void
		{
			CursorManager.setBusyCursor();
			var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0099", server,  DataLoaderFactory.TYPE_JSON_HTTP); 
			var param:Object = new Object();
			
			param["I_PERNR"] = pernr;
			param["I_PASSWORD"] = password;
			param["I_ACTION"] = "1";
			
			loader.load(param, resultLogin, faultLogin); 
		}

		public function chkLoginInfo(I_CPERNR:String, I_PASSWORD:String, I_PERNR:String):void
		{
			CursorManager.setBusyCursor();
			var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_Y001", server,  DataLoaderFactory.TYPE_JSON_HTTP); 
			var param:Object = new Object();
			
			param["I_CPERNR"] = I_CPERNR;
			param["I_PASSWORD"] = I_PASSWORD;
			param["I_PERNR"] = I_PERNR;
			
			loader.load(param, resultChkLogin, faultLogin); 
		}		
		
		
		
		public function changePassword(pernr:String, password:String, newPass:String ) :void
		{
			CursorManager.setBusyCursor();
			var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0099", server); 
			var param:Object = new Object();
			
			param["I_PERNR"] 		= pernr;
			param["I_PASSWORD"] 	= password;
			param["I_NEWPASSWORD"] 	= newPass;
			param["I_VERIPASSWORD"] = newPass;
			param["I_ACTION"] 		= "2";
			
			loader.load(param, resultChange, faultChange); 
		}
		
		
		
		private function resultLogin(event:ResultEvent):void
		{
			retCode = event.result["RETCODE"];
			retText = event.result["RETTEXT"];
			var code:int = event.result["VALCODE"];
			if(retCode == 0) {
				if(code == 0) {
					dispatchEvent(new LoginEvent(LoginEvent.SUCCEED_LOGIN, code, event.result["VALTEXT"]));
				}	
				else {
					dispatchEvent(new LoginEvent(LoginEvent.FAILED_LOGIN, code, event.result["VALTEXT"]));
				}
			}
			else {
				dispatchEvent(new LoginEvent(LoginEvent.FAILED_LOGIN, retCode, retText));
			}
			trace(code, event.result["VALTEXT"]);
			CursorManager.removeBusyCursor();
		}
		
		private function resultChkLogin(event:ResultEvent):void
		{
			retCode = event.result["RETCODE"];
			retText = event.result["RETTEXT"];
			var code:int = event.result["VALCODE"];
			if(retCode == 0) {
				if(code == 0) {
					dispatchEvent(new LoginEvent(LoginEvent.SUCCEED_CHK, code, event.result["VALTEXT"]));
				}	
				else {
					dispatchEvent(new LoginEvent(LoginEvent.FAILED_CHK, code, event.result["VALTEXT"]));
				}
			}
			else {
				dispatchEvent(new LoginEvent(LoginEvent.FAILED_CHK, retCode, retText));
			}
			trace(code, event.result["VALTEXT"]);
			CursorManager.removeBusyCursor();
		}
		
		private function faultLogin(event:FaultEvent):void
		{
			retCode = -1;
			retText = "["+event.fault.faultCode+"]"+event.fault.faultString;
			faultDetail = event.fault.faultDetail;
			dispatchEvent(new LoginEvent(LoginEvent.FAILED_LOGIN, retCode, retText));
			CursorManager.removeBusyCursor();
		}
		
		private function resultChange(event:ResultEvent):void
		{
			retCode = event.result["RETCODE"];
			retText = event.result["RETTEXT"];
			var code:int = event.result["VALCODE"];
			if(retCode == 0) {
				if(code == 0) {
					dispatchEvent(new LoginEvent(LoginEvent.SUCCEED_CHANGEPASS, code, event.result["VALTEXT"]));
				}	
				else {
					dispatchEvent(new LoginEvent(LoginEvent.FAILED_CHANGEPASS, code, event.result["VALTEXT"]));
				}
			}
			else {
				dispatchEvent(new LoginEvent(LoginEvent.FAILED_CHANGEPASS, retCode, retText));
			}
			trace(code, event.result["VALTEXT"]);
			CursorManager.removeBusyCursor();
		}
		
		private function faultChange(event:FaultEvent):void
		{
			retCode = -1;
			retText = "["+event.fault.faultCode+"]"+event.fault.faultString;
			faultDetail = event.fault.faultDetail;
			dispatchEvent(new LoginEvent(LoginEvent.FAILED_CHANGEPASS, retCode, retText));
			CursorManager.removeBusyCursor();
		}
		
	}
}